﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Commons.Utils;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Inp;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Dao;
using Shpm.System;

public partial class App_Input_PersonTel : System.Web.UI.Page
{

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["PROC_ID"] = Context.Items["SALE_PROC_ID"];
            SetCmbType();
            if (this.ViewState["PROC_ID"].ToString().Length > 0)
            {
                bindSelectData(this.ViewState["PROC_ID"].ToString());
            }
            
        }
    }
    #endregion

    #region 设置标签
    protected void lnkTab_Command(object sender, CommandEventArgs e)
    {
        string directUrl = "~/App/Input/SaleProcesseMembershipForm.aspx";
        switch (e.CommandName)
        {
            case "btn0":
                // 新建洽谈
                // this.LinkButton1.CssClass = "selectli";
                // this.LinkButton2.CssClass = "";
                directUrl = "~/App/Input/SaleProcessBeginForm.aspx";
                break;
            case "btn1":
                // 销售管理
                Context.Items["RIGHT"] = "true";
                directUrl = "~/App/Input/PersonVst.aspx";
                break;
            case "btn2":
                // 入住管理
                directUrl = "~/App/Input/MemberForm.aspx";
                break;
            case "btn3":
                // 购卡管理
                directUrl = "~/App/Input/RightPersonForm.aspx";
                break;
            case "btn4":
                // 客户移交
                directUrl = "~/App/Input/ContractHabitationForm.aspx";
                break;
            case "btn1_1":
                // 上门拜访
                directUrl = "~/App/Input/PersonVstSrh.aspx";
                break;
            case "btn1_2":
                // 日常交流
                directUrl = "~/App/Input/PersonTel.aspx";
                break;
            case "btn1_3":
                // 礼品发放
                directUrl = "~/App/Input/SaleProcessPresentForm.aspx";
                break;
            case "btn6":
                // 资格审核
                directUrl = "~/App/Input/SaleProcessIndefinForm.aspx";
                break;
            default:
                break;
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["PROC_ID"])))
        {
            Context.Items["SALE_PROC_ID"] = this.ViewState["PROC_ID"];
        }
        ControllerUtils.Transfer(this.Context, directUrl, false);
    }
    #endregion

    #region 读取数据
    // bind select data by procId
    private void bindSelectData(string procId)
    {
        SaleProcessDao dao = new SaleProcessDao();
        SaleProcessBean beanSP = new SaleProcessBean();
        beanSP.SaleProcId = Convert.ToInt32(procId);
        beanSP = dao.FindBySaleProcess(beanSP);
        if (beanSP != null)
        {
            lblProcId.Text = beanSP.SaleProcId.ToString();
            lblEmpId.Text = string.Format("{0} {1}", beanSP.EmployeeId, beanSP.EmployeeName);
            lblName.Text = beanSP.RelationName;
            lblTel.Text = beanSP.RelationTelephone;
            lblMail.Text = beanSP.RelationEmail;
            lblAddress.Text = beanSP.RelationAddress;
        }
    }
    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        if (this.ViewState["PROC_ID"].ToString().Length > 0)
        {
            // get 履历信息
            SaleProcessDao dao = new SaleProcessDao();
            InpSalesVistingBean beanV = new InpSalesVistingBean();
            beanV.SaleProcId = Convert.ToInt32(this.ViewState["PROC_ID"]);
            IList listV = dao.ListSaleProcVisitHistory(beanV);
            viewSrhRst.DataSource = listV;
            viewSrhRst.DataBind();
        }

        // 销售过程跟踪 link 状态
        //setLinkStatus();
    }
    #endregion

    #region 设置下拉框
    private void SetCmbType()
    {
        CommonDictionaryBean paramBean = new CommonDictionaryBean();
        paramBean.Catalog = "T_SALES_VISITING.SALE_VISITING_TYPE";
        CommonDictionaryDao dao = new CommonDictionaryDao();
        IList dataList = dao.FindByCatalog(paramBean);
        cmbType.DataSource = dataList;
        cmbType.DataBind();
        if (SetCmbIndex(dataList) != -1)
            cmbType.SelectedIndex = SetCmbIndex(dataList);
    }
    //设置下拉框的默认值
    private int SetCmbIndex(IList list)
    {
        CommonDictionaryBean dataBean = new CommonDictionaryBean();
        for (int i = 0; i < list.Count; i++)
        {
            dataBean = (CommonDictionaryBean)list[i];
            if (dataBean.Default == true) return i;
        }
        return -1;
    }
    #endregion

    #region 删除按钮
    protected void viewSrhRst_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            GridViewRow row = viewSrhRst.SelectedRow;
            int IntVal = Convert.ToInt32(((Label)row.Cells[0].FindControl("Label1")).Text);
            SaleProcessDao dao = new SaleProcessDao();
            InpSalesVistingBean beanV = new InpSalesVistingBean();
            beanV.SaleVisitingId = IntVal;
            dao.Delete(beanV);
        }
        catch (Exception ex)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0203").ToString().Replace("{0}", "回访信息履历");
        }

    }
    #endregion

    #region 页数设置
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        viewSrhRst.PageIndex = e.NewPageIndex;
    }
    #endregion

    #region 登录按钮
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
            string emplyRole = mstEmpBean == null ? "" : mstEmpBean.Role;
            if (checkAuthority(this.ViewState["PROC_ID"].ToString(), emplyId, emplyRole))
            {
                SaleProcessDao dao = new SaleProcessDao();
                InpSalesVistingBean beanSP = new InpSalesVistingBean();
                
                beanSP.SaleProcId = Convert.ToInt32(ViewState["PROC_ID"]);
                beanSP.CancelFlg = false;
                beanSP.Memo = txtMemo.Text;
                beanSP.SaleVisitingType = cmbType.SelectedValue;
                beanSP.UserId = loginId;
                dao.InsertHistory(beanSP);
                ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
            }
            else
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0901").ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion

    #region 检查权限
    private Boolean checkAuthority(string procId, string emplyId, string role)
    {
        SaleProcessDao daoCheck = new SaleProcessDao();
        SaleProcessBean bean = new SaleProcessBean();
        bean.SaleProcId = Convert.ToInt32(procId);
        bean.EmployeeId = emplyId;
        bean.Role = role;
        IList list = daoCheck.CheckSaleProcessAu(bean);
        if (list == null || list.Count == 0) return false;
        return true;
    }
    #endregion
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Context.Items["SALE_PROC_ID"] = this.ViewState["PROC_ID"];
        string directUrl = "~/App/Input/SaleCountForm.aspx";
        ControllerUtils.Transfer(this.Context, directUrl, false);
    }
}
